from __future__ import annotations

from typing import Optional

from redis.asyncio import Redis

from app.core.config import settings

_redis: Optional[Redis] = None


def get_redis() -> Redis:
  global _redis
  if _redis is None:
    _redis = Redis.from_url(
      settings.redis_url,
      encoding='utf-8',
      decode_responses=True,
      socket_connect_timeout=2.0,  # fail fast if redis not reachable
      socket_timeout=2.0,          # ops timeout
      retry_on_timeout=False,
    )
  return _redis
